package com.wc.算法提高课.A第一章_动态规划.背包模型.宠物小精灵之收服;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/9 15:11
 * @description https://www.acwing.com/problem/content/1024/
 */
public class Main {
    /**
     * 01背包
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, M = 510;
    static int[][] f = new int[N][M];
    static int n, m, k;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        k = sc.nextInt();
        while (k-- > 0) {
            int w = sc.nextInt(), v = sc.nextInt();
            for (int i = n; i >= w; i--) {
                for (int j = m; j >= v + 1; j--) {
                    f[i][j] = Math.max(f[i][j], f[i - w][j - v] + 1);
                }
            }
        }
        int maxV = 0, maxM = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j < m; j++) {
                if (maxV < f[i][j]) {
                    maxV = f[i][j];
                    maxM = m - j;
                } else if (maxV == f[i][j]) {
                    maxM = Math.max(m - j, maxM);
                }
            }
        }
        // 这个maxM其实相当于每一个宠物其实默认都是消耗了maxM + 1个的，因为体力是从1开始的
        out.println(maxV + " " + (maxM + 1));
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
